#!/bin/bash

# Happy Mall Monorepo 开发工具安装脚本

set -e

echo "🔧 安装 Happy Mall 开发工具..."

# 检查 Go 版本
echo "📝 检查 Go 版本..."
if ! command -v go &> /dev/null; then
    echo "❌ Go 未安装，请先安装 Go 1.24+"
    exit 1
fi

GO_VERSION=$(go version | awk '{print $3}' | sed 's/go//')
if [[ "$(printf '%s\n' "1.24" "$GO_VERSION" | sort -V | head -n1)" != "1.24" ]]; then
    echo "❌ Go 版本过低，需要 1.24+，当前版本: $GO_VERSION"
    exit 1
fi

echo "✅ Go 版本检查通过: $GO_VERSION"

# 安装代码检查工具
echo "🔍 安装 golangci-lint..."
if ! command -v golangci-lint &> /dev/null; then
    curl -sSfL https://raw.githubusercontent.com/golangci/golangci-lint/master/install.sh | sh -s -- -b $(go env GOPATH)/bin v1.55.2
else
    echo "✅ golangci-lint 已安装"
fi

# 安装代码格式化工具
echo "🎨 安装 goimports..."
if ! command -v goimports &> /dev/null; then
    go install golang.org/x/tools/cmd/goimports@latest
else
    echo "✅ goimports 已安装"
fi

# 安装 gofumpt (更严格的格式化)
echo "🎨 安装 gofumpt..."
if ! command -v gofumpt &> /dev/null; then
    go install mvdan.cc/gofumpt@latest
else
    echo "✅ gofumpt 已安装"
fi

# 安装 buf (Protocol Buffers)
echo "📡 安装 buf..."
if ! command -v buf &> /dev/null; then
    if [[ "$OSTYPE" == "darwin"* ]]; then
        # macOS
        if command -v brew &> /dev/null; then
            brew install bufbuild/buf/buf
        else
            echo "❌ 请先安装 Homebrew 或手动安装 buf"
            exit 1
        fi
    elif [[ "$OSTYPE" == "linux-gnu"* ]]; then
        # Linux
        curl -sSL "https://github.com/bufbuild/buf/releases/latest/download/buf-$(uname -s)-$(uname -m)" -o /tmp/buf
        chmod +x /tmp/buf
        sudo mv /tmp/buf /usr/local/bin/buf
    else
        echo "❌ 不支持的操作系统: $OSTYPE"
        exit 1
    fi
else
    echo "✅ buf 已安装"
fi

# 安装 protoc-gen-go 和 protoc-gen-go-grpc
echo "📡 安装 protoc 插件..."
go install google.golang.org/protobuf/cmd/protoc-gen-go@latest
go install google.golang.org/grpc/cmd/protoc-gen-go-grpc@latest

# 安装 migrate 工具
echo "📊 安装 migrate..."
if ! command -v migrate &> /dev/null; then
    if [[ "$OSTYPE" == "darwin"* ]]; then
        if command -v brew &> /dev/null; then
            brew install golang-migrate
        else
            go install -tags 'postgres' github.com/golang-migrate/migrate/v4/cmd/migrate@latest
        fi
    else
        go install -tags 'postgres' github.com/golang-migrate/migrate/v4/cmd/migrate@latest
    fi
else
    echo "✅ migrate 已安装"
fi

# 安装 swag (Swagger 生成器)
echo "📚 安装 swag..."
if ! command -v swag &> /dev/null; then
    go install github.com/swaggo/swag/cmd/swag@latest
else
    echo "✅ swag 已安装"
fi

# 安装 air (热重载)
echo "🔥 安装 air..."
if ! command -v air &> /dev/null; then
    go install github.com/cosmtrek/air@latest
else
    echo "✅ air 已安装"
fi

# 安装 wire (依赖注入)
echo "🔌 安装 wire..."
if ! command -v wire &> /dev/null; then
    go install github.com/google/wire/cmd/wire@latest
else
    echo "✅ wire 已安装"
fi

# 检查 Docker
echo "🐳 检查 Docker..."
if ! command -v docker &> /dev/null; then
    echo "⚠️  Docker 未安装，请手动安装 Docker"
else
    echo "✅ Docker 已安装"
fi

# 检查 Docker Compose
echo "🐳 检查 Docker Compose..."
if ! command -v docker-compose &> /dev/null && ! docker compose version &> /dev/null; then
    echo "⚠️  Docker Compose 未安装，请手动安装"
else
    echo "✅ Docker Compose 已安装"
fi

# 创建 Git hooks
echo "🪝 设置 Git hooks..."
mkdir -p .git/hooks

cat > .git/hooks/pre-commit << 'EOF'
#!/bin/bash
echo "🔍 运行预提交检查..."
make format
make lint
EOF

chmod +x .git/hooks/pre-commit

echo "🎉 所有工具安装完成！"
echo ""
echo "📋 已安装的工具:"
echo "  - golangci-lint: 代码检查"
echo "  - goimports: 导入格式化"
echo "  - gofumpt: 代码格式化"
echo "  - buf: Protocol Buffers"
echo "  - migrate: 数据库迁移"
echo "  - swag: Swagger 文档生成"
echo "  - air: 热重载开发"
echo "  - wire: 依赖注入"
echo ""
echo "🚀 现在可以开始开发了！运行 'make help' 查看可用命令。"
